#include <algorithm>
#include <cstdio>
#include <cstring>

typedef long long ll;
const int N = 2e5 + 5, mod = 1e9 + 7;
int cvt[N], dfn[N];
int n, q, tot, rt, cnt;
ll val[N << 2];

struct Node {
  int ch[2], fa, siz;
  ll sum, val;
} tr[N];

inline void pushup(int x) { val[x] = val[x << 1] * val[x << 1 | 1] % mod; }

inline void update(int x) {
  tr[x].siz = tr[tr[x].ch[0]].siz + tr[tr[x].ch[1]].siz + 1;
  tr[x].sum = (tr[tr[x].ch[0]].sum + tr[tr[x].ch[1]].sum + tr[x].val) % mod;
}

inline void dfs(int x) {
  if (!x) return;
  dfs(tr[x].ch[0]);
  cvt[dfn[x] = ++tot] = x;
  dfs(tr[x].ch[1]);
  update(x);
}

inline void build(int x, int l, int r) {
  if (l == r) { return val[x] = tr[cvt[l]].sum, void(); }
  int mid = (l + r) >> 1;
  build(x << 1, l, mid);
  build(x << 1 | 1, mid + 1, r);
  pushup(x);
}

inline void modify(int x, int l, int r, int p) {
  if (l == r) { return val[x] = tr[cvt[p]].sum, void(); }
  int mid = (l + r) >> 1;
  if (p <= mid) modify(x << 1, l, mid, p);
  else
    modify(x << 1 | 1, mid + 1, r, p);
  pushup(x);
}

inline ll query(int x, int l, int r, int L, int R) {
  if (L <= l && r <= R) return val[x];
  int mid = (l + r) >> 1;
  ll res = 1;
  if (L <= mid) res = query(x << 1, l, mid, L, R);
  if (R > mid) res *= query(x << 1 | 1, mid + 1, r, L, R);
  return res >= mod ? res % mod : res;
}

inline bool iden(int x) { return tr[tr[x].fa].ch[1] == x; }

inline void rotate(int x) {
  bool xtofa = iden(x);
  int fa = tr[x].fa, ffa = tr[fa].fa, son = tr[x].ch[!xtofa];
  if (ffa) tr[ffa].ch[iden(fa)] = x;
  tr[x].fa = ffa;
  tr[x].ch[!xtofa] = fa;
  tr[fa].ch[xtofa] = son;
  tr[fa].fa = x;
  if (son) tr[son].fa = fa;
  update(fa);
  update(x);
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("testdata.in", "r", stdin);
  freopen("testdata.out", "w", stdout);
#else
  freopen("splay.in", "r", stdin);
  freopen("splay.out", "w", stdout);
#endif
#endif

  scanf("%d%d", &n, &q);
  for (int i = 1; i <= n; ++i) {
    scanf("%lld%d%d", &tr[i].val, &tr[i].ch[0], &tr[i].ch[1]);
    if (tr[i].ch[0]) tr[tr[i].ch[0]].fa = i;
    if (tr[i].ch[1]) tr[tr[i].ch[1]].fa = i;
  }
  dfs(1);
  build(1, 1, tot);
  for (int opt, x; q--;) {
    scanf("%d%d", &opt, &x);
    if (opt < 2) {
      if (tr[x].ch[opt]) x = tr[x].ch[opt];
      else
        continue;
      rotate(x);
      modify(1, 1, tot, dfn[x]);
      if ((x = tr[x].ch[!opt])) modify(1, 1, tot, dfn[x]);
    } else
      printf("%lld\n", query(1, 1, tot, dfn[x] - tr[tr[x].ch[0]].siz,
                             dfn[x] + tr[tr[x].ch[1]].siz));
  }
  return 0;
}